package cn.ibizlab.odoo.core.odoo_note.service.impl;

import java.io.Serializable;
import java.sql.Timestamp;
import java.util.ArrayList;
import java.util.List;
import java.util.Map;
import java.util.HashMap;
import java.math.BigInteger;
import java.util.Collection;

import lombok.extern.slf4j.Slf4j;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.cglib.beans.BeanCopier;
import org.springframework.stereotype.Service;
import org.springframework.data.domain.Page;
import org.springframework.data.domain.PageImpl;
import org.springframework.data.domain.Pageable;
import cn.ibizlab.odoo.core.client.model.Inote_note;
import cn.ibizlab.odoo.core.odoo_note.domain.Note_note;
import cn.ibizlab.odoo.core.odoo_note.filter.Note_noteSearchContext;
import cn.ibizlab.odoo.core.odoo_note.service.INote_noteService;
import cn.ibizlab.odoo.util.SearchResult;
import cn.ibizlab.odoo.util.log.IBIZLog;

/**
 * 实体[便签] 服务对象接口实现
 */
@Slf4j
@IBIZLog
@Service
public class Note_noteServiceImpl implements INote_noteService {

    @Autowired
    private cn.ibizlab.odoo.core.client.service.Inote_noteClientService note_noteClientService;

    @Override
    public void updateBatch(List<Note_note> note_notes, int batchSize) {

        //note_noteClientService.updateBatch(note_notes,batchSize) ;

    }

    @Override
    public Note_note get(Integer id) {
        Inote_note clientModel = note_noteClientService.createModel();
        clientModel.setId(id);
        note_noteClientService.get(clientModel) ;
        return this.convert2Domain(clientModel,null);
    }

    @Override
    public Note_note get(Note_note note_note) {
        Note_note retnote_note = this.get(note_note.getId()) ;
    	BeanCopier copier=BeanCopier.create(retnote_note.getClass(), note_note.getClass(), false);
        copier.copy(retnote_note, note_note, null);
        return note_note ;
    }

    @Override
    public boolean remove(Integer id) {
        boolean bOk = false ;
        Inote_note clientModel = note_noteClientService.createModel();
        clientModel.setId(id);
        note_noteClientService.remove(clientModel) ;
        bOk = true ;
        return bOk ;
    }

    @Override
    public boolean create(Note_note note_note) {
        boolean bOk = false ;
        Inote_note clientModel = this.convert2Model(note_note,null);
        note_noteClientService.create(clientModel) ;
        this.convert2Domain(clientModel,note_note);
        bOk = true ;

        return bOk ;
    }

    @Override
    public void removeBatch(Collection<? extends Serializable> idList, int batchSize) {

        //note_noteClientService.removeBatch(note_notes) ;

    }

    @Override
    public boolean update(Note_note note_note) {
        boolean bOk = false ;
        Inote_note clientModel = this.convert2Model(note_note,null);
        note_noteClientService.update(clientModel) ;
        this.convert2Domain(clientModel,note_note) ;
        bOk = true;
        return bOk ;
    }

    @Override
    public void createBatch(List<Note_note> note_notes, int batchSize) {

                        //note_noteClientService.createBatch(note_notes,batchSize) ;

    }

    /**
     * 查询集合 默认查询
     */
    @Override
    public Page<Note_note> searchDefault(Note_noteSearchContext context) {
        List<Note_note> list = new ArrayList<Note_note>();
        Page<cn.ibizlab.odoo.core.client.model.Inote_note> clientModelList = note_noteClientService.search(context) ;
        for(cn.ibizlab.odoo.core.client.model.Inote_note clientModel : clientModelList){
            list.add( this.convert2Domain(clientModel,null));
        }
        return new PageImpl<Note_note>(list, context.getPageable(), clientModelList.getTotalElements());
    }


    /**
     * 根据用户查询
     */
    @Override
    public Page<Note_note> selectByOdoo_create(Integer id){
        Note_noteSearchContext context = new Note_noteSearchContext();
        if(!org.springframework.util.StringUtils.isEmpty(id)){
            cn.ibizlab.odoo.util.SearchFieldFilter fieldFilter=new cn.ibizlab.odoo.util.SearchFieldFilter();
            fieldFilter.setParam("id");
            fieldFilter.setCondition(cn.ibizlab.odoo.util.enums.SearchFieldType.EQ);
            fieldFilter.setValue(id);
            context.getCondition().add(fieldFilter);
        }
        List<Note_note> list = new ArrayList<Note_note>();
        Page<cn.ibizlab.odoo.core.client.model.Inote_note> clientModelList = note_noteClientService.search(context) ;
        for(cn.ibizlab.odoo.core.client.model.Inote_note clientModel : clientModelList){
            list.add( this.convert2Domain(clientModel,null));
        }
        return new PageImpl<Note_note>(list, context.getPageable(), clientModelList.getTotalElements());

    }

    /**
     * 根据用户查询
     */
    @Override
    public Page<Note_note> selectByOdoo_user(Integer id){
        Note_noteSearchContext context = new Note_noteSearchContext();
        if(!org.springframework.util.StringUtils.isEmpty(id)){
            cn.ibizlab.odoo.util.SearchFieldFilter fieldFilter=new cn.ibizlab.odoo.util.SearchFieldFilter();
            fieldFilter.setParam("id");
            fieldFilter.setCondition(cn.ibizlab.odoo.util.enums.SearchFieldType.EQ);
            fieldFilter.setValue(id);
            context.getCondition().add(fieldFilter);
        }
        List<Note_note> list = new ArrayList<Note_note>();
        Page<cn.ibizlab.odoo.core.client.model.Inote_note> clientModelList = note_noteClientService.search(context) ;
        for(cn.ibizlab.odoo.core.client.model.Inote_note clientModel : clientModelList){
            list.add( this.convert2Domain(clientModel,null));
        }
        return new PageImpl<Note_note>(list, context.getPageable(), clientModelList.getTotalElements());

    }

    /**
     * 根据用户查询
     */
    @Override
    public Page<Note_note> selectByOdoo_write(Integer id){
        Note_noteSearchContext context = new Note_noteSearchContext();
        if(!org.springframework.util.StringUtils.isEmpty(id)){
            cn.ibizlab.odoo.util.SearchFieldFilter fieldFilter=new cn.ibizlab.odoo.util.SearchFieldFilter();
            fieldFilter.setParam("id");
            fieldFilter.setCondition(cn.ibizlab.odoo.util.enums.SearchFieldType.EQ);
            fieldFilter.setValue(id);
            context.getCondition().add(fieldFilter);
        }
        List<Note_note> list = new ArrayList<Note_note>();
        Page<cn.ibizlab.odoo.core.client.model.Inote_note> clientModelList = note_noteClientService.search(context) ;
        for(cn.ibizlab.odoo.core.client.model.Inote_note clientModel : clientModelList){
            list.add( this.convert2Domain(clientModel,null));
        }
        return new PageImpl<Note_note>(list, context.getPageable(), clientModelList.getTotalElements());

    }



    public Inote_note convert2Model(Note_note domain , Inote_note model) {
        if(domain == null)
            return model ;
        if(model==null){
            model = note_noteClientService.createModel() ;
        }

         if(domain.getMessage_has_error_counterDirtyFlag())
            model.setMessage_has_error_counter(domain.getMessage_has_error_counter());
         if(domain.getMemoDirtyFlag())
            model.setMemo(domain.getMemo());
         if(domain.getMessage_attachment_countDirtyFlag())
            model.setMessage_attachment_count(domain.getMessage_attachment_count());
         if(domain.getIdDirtyFlag())
            model.setId(domain.getId());
         if(domain.getStage_idsDirtyFlag())
            model.setStage_ids(domain.getStage_ids());
         if(domain.getOpenDirtyFlag())
            model.setOpen(domain.getOpen());
         if(domain.getDate_doneDirtyFlag())
            model.setDate_done(domain.getDate_done());
         if(domain.getMessage_follower_idsDirtyFlag())
            model.setMessage_follower_ids(domain.getMessage_follower_ids());
         if(domain.getMessage_channel_idsDirtyFlag())
            model.setMessage_channel_ids(domain.getMessage_channel_ids());
         if(domain.getCreate_dateDirtyFlag())
            model.setCreate_date(domain.getCreate_date());
         if(domain.getColorDirtyFlag())
            model.setColor(domain.getColor());
         if(domain.getMessage_needactionDirtyFlag())
            model.setMessage_needaction(domain.getMessage_needaction());
         if(domain.getActivity_type_idDirtyFlag())
            model.setActivity_type_id(domain.getActivity_type_id());
         if(domain.getMessage_needaction_counterDirtyFlag())
            model.setMessage_needaction_counter(domain.getMessage_needaction_counter());
         if(domain.getMessage_idsDirtyFlag())
            model.setMessage_ids(domain.getMessage_ids());
         if(domain.getActivity_summaryDirtyFlag())
            model.setActivity_summary(domain.getActivity_summary());
         if(domain.getWrite_dateDirtyFlag())
            model.setWrite_date(domain.getWrite_date());
         if(domain.getMessage_has_errorDirtyFlag())
            model.setMessage_has_error(domain.getMessage_has_error());
         if(domain.getTag_idsDirtyFlag())
            model.setTag_ids(domain.getTag_ids());
         if(domain.getMessage_partner_idsDirtyFlag())
            model.setMessage_partner_ids(domain.getMessage_partner_ids());
         if(domain.getActivity_idsDirtyFlag())
            model.setActivity_ids(domain.getActivity_ids());
         if(domain.get__last_updateDirtyFlag())
            model.set__last_update(domain.get__last_update());
         if(domain.getSequenceDirtyFlag())
            model.setSequence(domain.getSequence());
         if(domain.getMessage_unreadDirtyFlag())
            model.setMessage_unread(domain.getMessage_unread());
         if(domain.getActivity_user_idDirtyFlag())
            model.setActivity_user_id(domain.getActivity_user_id());
         if(domain.getNameDirtyFlag())
            model.setName(domain.getName());
         if(domain.getMessage_main_attachment_idDirtyFlag())
            model.setMessage_main_attachment_id(domain.getMessage_main_attachment_id());
         if(domain.getMessage_is_followerDirtyFlag())
            model.setMessage_is_follower(domain.getMessage_is_follower());
         if(domain.getMessage_unread_counterDirtyFlag())
            model.setMessage_unread_counter(domain.getMessage_unread_counter());
         if(domain.getStage_idDirtyFlag())
            model.setStage_id(domain.getStage_id());
         if(domain.getDisplay_nameDirtyFlag())
            model.setDisplay_name(domain.getDisplay_name());
         if(domain.getWebsite_message_idsDirtyFlag())
            model.setWebsite_message_ids(domain.getWebsite_message_ids());
         if(domain.getActivity_date_deadlineDirtyFlag())
            model.setActivity_date_deadline(domain.getActivity_date_deadline());
         if(domain.getActivity_stateDirtyFlag())
            model.setActivity_state(domain.getActivity_state());
         if(domain.getUser_id_textDirtyFlag())
            model.setUser_id_text(domain.getUser_id_text());
         if(domain.getCreate_uid_textDirtyFlag())
            model.setCreate_uid_text(domain.getCreate_uid_text());
         if(domain.getWrite_uid_textDirtyFlag())
            model.setWrite_uid_text(domain.getWrite_uid_text());
         if(domain.getWrite_uidDirtyFlag())
            model.setWrite_uid(domain.getWrite_uid());
         if(domain.getCreate_uidDirtyFlag())
            model.setCreate_uid(domain.getCreate_uid());
         if(domain.getUser_idDirtyFlag())
            model.setUser_id(domain.getUser_id());
        return model ;
    }

    public Note_note convert2Domain( Inote_note model ,Note_note domain) {
        if(model == null)
            return domain ;
        if(domain == null){
            domain = new Note_note();
        }

        if(model.getMessage_has_error_counterDirtyFlag())
            domain.setMessage_has_error_counter(model.getMessage_has_error_counter());
        if(model.getMemoDirtyFlag())
            domain.setMemo(model.getMemo());
        if(model.getMessage_attachment_countDirtyFlag())
            domain.setMessage_attachment_count(model.getMessage_attachment_count());
        if(model.getIdDirtyFlag())
            domain.setId(model.getId());
        if(model.getStage_idsDirtyFlag())
            domain.setStage_ids(model.getStage_ids());
        if(model.getOpenDirtyFlag())
            domain.setOpen(model.getOpen());
        if(model.getDate_doneDirtyFlag())
            domain.setDate_done(model.getDate_done());
        if(model.getMessage_follower_idsDirtyFlag())
            domain.setMessage_follower_ids(model.getMessage_follower_ids());
        if(model.getMessage_channel_idsDirtyFlag())
            domain.setMessage_channel_ids(model.getMessage_channel_ids());
        if(model.getCreate_dateDirtyFlag())
            domain.setCreate_date(model.getCreate_date());
        if(model.getColorDirtyFlag())
            domain.setColor(model.getColor());
        if(model.getMessage_needactionDirtyFlag())
            domain.setMessage_needaction(model.getMessage_needaction());
        if(model.getActivity_type_idDirtyFlag())
            domain.setActivity_type_id(model.getActivity_type_id());
        if(model.getMessage_needaction_counterDirtyFlag())
            domain.setMessage_needaction_counter(model.getMessage_needaction_counter());
        if(model.getMessage_idsDirtyFlag())
            domain.setMessage_ids(model.getMessage_ids());
        if(model.getActivity_summaryDirtyFlag())
            domain.setActivity_summary(model.getActivity_summary());
        if(model.getWrite_dateDirtyFlag())
            domain.setWrite_date(model.getWrite_date());
        if(model.getMessage_has_errorDirtyFlag())
            domain.setMessage_has_error(model.getMessage_has_error());
        if(model.getTag_idsDirtyFlag())
            domain.setTag_ids(model.getTag_ids());
        if(model.getMessage_partner_idsDirtyFlag())
            domain.setMessage_partner_ids(model.getMessage_partner_ids());
        if(model.getActivity_idsDirtyFlag())
            domain.setActivity_ids(model.getActivity_ids());
        if(model.get__last_updateDirtyFlag())
            domain.set__last_update(model.get__last_update());
        if(model.getSequenceDirtyFlag())
            domain.setSequence(model.getSequence());
        if(model.getMessage_unreadDirtyFlag())
            domain.setMessage_unread(model.getMessage_unread());
        if(model.getActivity_user_idDirtyFlag())
            domain.setActivity_user_id(model.getActivity_user_id());
        if(model.getNameDirtyFlag())
            domain.setName(model.getName());
        if(model.getMessage_main_attachment_idDirtyFlag())
            domain.setMessage_main_attachment_id(model.getMessage_main_attachment_id());
        if(model.getMessage_is_followerDirtyFlag())
            domain.setMessage_is_follower(model.getMessage_is_follower());
        if(model.getMessage_unread_counterDirtyFlag())
            domain.setMessage_unread_counter(model.getMessage_unread_counter());
        if(model.getStage_idDirtyFlag())
            domain.setStage_id(model.getStage_id());
        if(model.getDisplay_nameDirtyFlag())
            domain.setDisplay_name(model.getDisplay_name());
        if(model.getWebsite_message_idsDirtyFlag())
            domain.setWebsite_message_ids(model.getWebsite_message_ids());
        if(model.getActivity_date_deadlineDirtyFlag())
            domain.setActivity_date_deadline(model.getActivity_date_deadline());
        if(model.getActivity_stateDirtyFlag())
            domain.setActivity_state(model.getActivity_state());
        if(model.getUser_id_textDirtyFlag())
            domain.setUser_id_text(model.getUser_id_text());
        if(model.getCreate_uid_textDirtyFlag())
            domain.setCreate_uid_text(model.getCreate_uid_text());
        if(model.getWrite_uid_textDirtyFlag())
            domain.setWrite_uid_text(model.getWrite_uid_text());
        if(model.getWrite_uidDirtyFlag())
            domain.setWrite_uid(model.getWrite_uid());
        if(model.getCreate_uidDirtyFlag())
            domain.setCreate_uid(model.getCreate_uid());
        if(model.getUser_idDirtyFlag())
            domain.setUser_id(model.getUser_id());
        return domain ;
    }


}
